package models

import (
	"github.com/beego/beego/v2/core/logs"
	"rate/common/exception"
	"rate/common/util/secret"
	"rate/common/util/seqUtils"
	"rate/nps"
	"rate/pojo/entity"
)

// CreateClient 添加客户端
func CreateClient(userId int64, area int) error {
	// 判断客户端是否存在
	//c, err := FindClientByUserId(userId)
	//if err != nil {
	//	return err
	//}
	if true {
		client := entity.Client{
			Id:              seqUtils.GetSnow(),
			UserId:          userId,
			Vkey:            secret.GetSecret(),
			ConfigConnAllow: 1,
			Compress:        1,
			Crypt:           1,
		}
		// 调用接口
		nps.CreateClientApi(client, area)
		//if _, err := Orm.Insert(&client); err != nil {
		//	logs.Error("insert client error, %s", err.Error())
		//	return exception.NewSystemError()
		//}
	}
	return nil
}

// FindClientByUserId 查找用户客户端
func FindClientByUserId(userId int64) (*entity.Client, error) {
	client := entity.Client{UserId: userId}
	if err := Orm.Read(&client, "UserId"); err != nil {
		logs.Error("find member by name error, %s", err.Error())
		return nil, exception.NewSystemError()
	}
	return &client, nil
}
